package com.qf.dao.impl;

import com.mchange.v1.db.sql.ConnectionUtils;
import com.qf.dao.AccountDao;
import com.qf.pojo.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName AccountDaoImpl
 * @Description TODO
 * @Author 86181
 * @Date 2022/8/31 10:08
 * @Version 1.0
 **/
public class AccountDaoImpl implements AccountDao {
    private QueryRunner runner;
    public void setRunner(QueryRunner runner){
        this.runner=runner;
    }
    //保证连接是同一个
    private ConnectionUtils connectionUtils;
    public void  setConnectionUtils(ConnectionUtils connectionUtils){
        this.connectionUtils=connectionUtils;
    }
    public Account findAccountByName(String name) {
        try {
            List<Account> list =
                    runner.query("select * from account where name  =?", new BeanListHandler<Account>(Account.class), name);

            if(list==null||list.size()==0){
                return null;
            }
            if(list.size() > 1){
                throw new RuntimeException();
            }
            return list.get(0);


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public void updateAccount(Account account) {
        String sql = "update account set money=? where name=?";
        try {
            runner.update(sql,account.getMoney(),account.getName());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    }

